package one.stand.controller;

import io.swagger.annotations.Api;
import com.jmp.api.dto.VersionRequest;
import com.jmp.api.dto.VersionResponse;
import one.stand.model.ResultModel;
import one.stand.model.SysDictValue;
import one.stand.service.sys.SysDictValueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/version")
@Api(description = "版本更新接口")
public class VersionController {

    @Autowired
    private SysDictValueService sysDictValueService;

    @PostMapping("")
    public ResultModel<VersionResponse> version(@RequestAttribute VersionRequest request) {
        SysDictValue sysDictValue = null;
        if (StringUtils.isNoneBlank(request.getVersion()) && StringUtils.isNoneBlank(request.getCompanyKey())) {
            sysDictValue = sysDictValueService.get(SysDictValueService.Type.VERSION_JXS_APP, request.getCompanyKey());
        }

        boolean isUpdate = false;
        String apkUrl = StringUtils.EMPTY;
        if (Objects.nonNull(sysDictValue)) {
            isUpdate = request.getVersion().compareTo(sysDictValue.getValue()) < 0;
            apkUrl = sysDictValue.getMemo();
        }

        VersionResponse response = new VersionResponse();
        response.setUpdate(isUpdate);
        response.setAppUrl(apkUrl);
        return ResultModel.success(response);
    }
}
